﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp_Animation
{
    /// <summary>
    /// LinearAniamationWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LinearAniamationWindow : Window
    {
        public LinearAniamationWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //StartAniamation1();
            StartAnimation2();
        }

        /// <summary>
        /// 开始动画方式一,不好的地方，动画是分开调用的，不一定是同一时刻开始执行的
        /// </summary>
        private void StartAniamation1()
        {
            DoubleAnimation widthAnimation = new DoubleAnimation() 
            {
                Duration = new TimeSpan(0, 0, 5),
                From = 120,
                To = 300
            };
            DoubleAnimation heightAnimation = new DoubleAnimation() 
            {
                Duration = new TimeSpan(0, 0, 3),
                From = 30,
                To = 100
            };
            this.border.BeginAnimation(WidthProperty, widthAnimation);
            this.border.BeginAnimation(HeightProperty, heightAnimation);
        }

        /// <summary>
        /// 动画方式2，通过Storyboard控制板，多种动画同步开启
        /// </summary>
        private void StartAnimation2()
        {
            DoubleAnimation widthAnimation = new DoubleAnimation()
            {
                Duration = new TimeSpan(0, 0, 5),
                From = 120,
                To = 300
            };
            DoubleAnimation heightAnimation = new DoubleAnimation()
            {
                Duration = new TimeSpan(0, 0, 3),
                From = 30,
                To = 100
            };
            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(widthAnimation);
            storyboard.Children.Add(heightAnimation);
            Storyboard.SetTarget(widthAnimation, this.border);
            Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("Width"));
            Storyboard.SetTarget(heightAnimation, this.border);
            Storyboard.SetTargetProperty(heightAnimation, new PropertyPath("Height"));
            storyboard.Begin();
        }
    }
}
